using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Microsoft.PowerShell.CmdletManagement
{
    [System.Windows.Markup.ContentProperty("Label")]
    public partial class ExtendedCheckBox : System.Windows.Controls.UserControl
    {
        public ExtendedCheckBox()
        {
            InitializeComponent();

            CheckBox.IsEnabled = false;
        }

        public string Label
        {
            get { return (string) GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }

        public bool? Value
        {
            get { return (bool?) GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public string HelpText
        {
            get { return (string) GetValue(HelpTextProperty); }
            set { SetValue(HelpTextProperty, value); }
        }

        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }

        private void ValueChanged(object sender, RoutedEventArgs e)
        {
            if (Value != null)
            {
                CheckBox.IsThreeState = false;

                Binding binding = BindingOperations.GetBinding(CheckBox, CheckBox.IsCheckedProperty);
                binding.ValidationRules.Clear();
            }
            else
            {
                CheckBox.IsThreeState = true;

                Binding binding = BindingOperations.GetBinding(CheckBox, CheckBox.IsCheckedProperty);
                binding.ValidationRules.Clear();
                binding.ValidationRules.Add(new TriStateValidationRule());
            }
        }

        private static void OnIsReadOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ExtendedCheckBox checkBox = d as ExtendedCheckBox;

            if (((bool)e.NewValue) == true)
            {
                checkBox.CheckBox.IsEnabled = false;
            }
            else
            {
                checkBox.CheckBox.IsEnabled = true;
            }
        }

        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.Register("Label", typeof(string), typeof(ExtendedCheckBox));
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(bool?), typeof(ExtendedCheckBox));
        public static readonly DependencyProperty HelpTextProperty =
            DependencyProperty.Register("HelpText", typeof(string), typeof(ExtendedCheckBox));
        public static readonly DependencyProperty IsReadOnlyProperty =
            DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(ExtendedCheckBox),
            new FrameworkPropertyMetadata(true, new PropertyChangedCallback(OnIsReadOnlyChanged)));
     }

    public class TriStateValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            ValidationResult validationResult = new ValidationResult(true, null);

            if (value != null)
            {
                MessageBoxResult result = MessageBox.Show("Changing this property updates it for all parameter sets" +
                    " that contain it. This property currently varies per parameter set. Do you want to override those settings?",
                    "Override individual parameter set settings?",
                    MessageBoxButton.YesNo);


                if (result == MessageBoxResult.No)
                {
                    validationResult = new ValidationResult(false, "This value has not been applied to the individual parameter sets.");
                }
            }

            return validationResult;
        }
    }
}